import { _decorator, Component, Vec3, input, Input, EventKeyboard, KeyCode, Camera, BoxCollider2D, Contact2DType, RigidBody2D, Vec2, find, Prefab, instantiate, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass("PlayerController")
export class PlayerController extends Component {
    @property(Prefab)
    inCard: Prefab;
    @property(Node)
    cardNode: Node;
    @property(Prefab)
    plant: Prefab;
    private _left: boolean = false;
    private _right: boolean = false;
    // 跳跃步长
    private _jumpStep: number = 0;
    private _scrollTime: number = 0;
    // 当前角色位置
    private _curPos: Vec3 = new Vec3();
    // 角色目标位置
    private _targetPos: Vec3 = new Vec3();
    private _jumpCount: number = 0;
    private _startScroll: boolean = false;
    private _scrollSpeed: number;
    private _waitStop: boolean = false;
    private rigidBody: RigidBody2D;
    minDistance: Vec3 = new Vec3(60, 60, 0);
    start() {
        this.initCard()
        this.rigidBody = this.getComponent(RigidBody2D);
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
        let collider = this.node.getComponent(BoxCollider2D);
        // 监听触发事件
        collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    }
    initCard() {
        for (let index = 0; index < 5; index++) {
            let inCard = instantiate(this.inCard)
            //依次排开插入，中间无间隙
            inCard.setPosition(new Vec3(index * 26, 0, 0));
            let plant = instantiate(this.plant)
            plant.emit('plant')
            inCard.addChild(plant);
            this.cardNode.addChild(inCard);
        }
        
    }
    onBeginContact() {
        if (this._waitStop) {
            this._jumpCount = 0;
            this._waitStop = false;
        }
    }
    onKeyDown(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.SPACE:
                this._targetPos = new Vec3();
                this.jump(50);
                break;
            case KeyCode.KEY_A:
                this._targetPos = new Vec3();
                this.turnLeft(-20);
                break;
            case KeyCode.KEY_D:
                this._targetPos = new Vec3();
                this.turnRight(20);
                break;
        }
    }
    onKeyUp(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_A:
                this._left = false;
                if (this._jumpCount == 0) {
                    this.node.angle = 0;
                }

                break;
            case KeyCode.KEY_D:
                this._right = false;
                if (this._jumpCount == 0) {
                    this.node.angle = 0;
                }
                break;
        }
    }
    turnLeft(step: number) {

        if (this._left) {
            return;
        }
        let scaleX = this.node.scale;
        scaleX.set(-1, 1, 1);
        this._left = true;
        this._jumpStep = step;
        this.node.getPosition(this._curPos);
        Vec3.add(this._targetPos, this._curPos, new Vec3(this._jumpStep, 0, 0));
    }
    turnRight(step: number) {
        if (this._right) {
            return;
        }
        let scaleX = this.node.scale;
        scaleX.set(1, 1, 1);
        this._right = true;
        this._jumpStep = step;
        this.node.getPosition(this._curPos);
        Vec3.add(this._targetPos, this._curPos, new Vec3(this._jumpStep, 0, 0));
    }
    jump(step: number) {

        if (this._jumpCount > 1) {
            return;
        }
        if (this._jumpCount == 1) {
            this._scrollSpeed = -360 / 0.5;
            this._startScroll = true;
        }
        this.rigidBody.applyForceToCenter(new Vec2(0, step), true);
        this._waitStop = true;
        this._jumpCount++;
    }

    update(deltaTime: number) {
        if (this._startScroll) {
            this._scrollTime += deltaTime;
            if (this._scrollTime > 0.5) {
                this.node.angle = 0;
                this._startScroll = false;
                this._scrollTime = 0;
            } else {
                let angle = this._scrollTime * this._scrollSpeed;
                this.node.angle = angle;
            }
        }
        if (this._left) {
            // tween
            this.node.getPosition(this._curPos);
            Vec3.add(this._curPos, this._curPos, new Vec3(-100 * deltaTime, 50 * deltaTime, 0));
            this.node.setPosition(this._curPos);
            if (this._jumpCount == 0) {
                this.node.angle = 10;
            }

        }
        if (this._right) {
            // tween
            this.node.getPosition(this._curPos);
            Vec3.add(this._curPos, this._curPos, new Vec3(100 * deltaTime, 50 * deltaTime, 0));
            this.node.setPosition(this._curPos);
            if (this._jumpCount == 0) {
                this.node.angle = -10;
            }
        }
    }
}